package com.echat.core.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chentl on 17/3/12.
 * 用于对Object进行解析并且转换成Map键值对的形式
 *
 * @author chentl
 * @version 1.0
 */
public class ObjectUtils {

    /**
     * @return java.util.Map<java.lang.String               ,               java.lang.Object>
     * @author chentl
     * @description Bean --> Map
     * @date 2019/4/17 15:15
     * @params [obj]
     * @since JDK 1.7
     */
    public static Map<String, Object> transBean2Map(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!"class".equals(key)) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    map.put(key, value);
                }
            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }
        return map;
    }


    /**
     * @return void
     * @description 利用Introspector, PropertyDescriptor实现 Map --> Bean
     * @date 2019/4/17 15:14
     * @params [map, obj]
     * @since JDK 1.7
     */
    public static void transMap2Bean(Map<String, Object> map, Object obj) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property对应的setter方法
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }
            }
        } catch (Exception e) {
            System.out.println("transMap2Bean Error " + e);
        }
        return;
    }


    /**
     * 将 List<JavaBean>对象转化为List<Map>
     *
     * @return Object对象
     * @author wyply115
     * @version 2016年3月20日 11:03:01
     */
    public static List<Map<String, Object>> convertListBean2ListMap(List<?> beanList) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0, n = beanList.size(); i < n; i++) {
            Object bean = beanList.get(i);
            Map<String, Object> map = transBean2Map(bean);
            mapList.add(map);
        }
        return mapList;
    }
}
